//
//  BBPixelBlob.m
//  BBBlobDetectTestbed
//
//  Created by ben smith on 3/16/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBPixelBlob.h"

@implementation BBPixelBlob

@synthesize pixels;
@synthesize values;

@synthesize valueMax;
@synthesize valueMin;

@synthesize xMax;
@synthesize xMin;
@synthesize yMax;
@synthesize yMin;

@synthesize pixelMass;

@synthesize center;
@synthesize invert;

+(BBPixelBlob*)pixelBlob
{
	BBPixelBlob * newBlob = [[BBPixelBlob alloc] init];
	return [newBlob autorelease];
}

- (id)copyWithZone:(NSZone *)zone
{
	BBPixelBlob *copy = [[BBPixelBlob allocWithZone:zone] init];
	[copy setPixels:[[self pixels] copy]];
	[copy setValues:[[self values] copy]];
	[copy setXMax:xMax];
	[copy setXMin:xMin];
	[copy setYMax:yMax];
	[copy setYMin:yMin];

	[copy setPixelMass:pixelMass];
	[copy setCenter:center];
	return copy;
}


- (id) init
{
	self = [super init];
	if (self != nil) {
		pixels = [[NSMutableArray alloc] init];
		values = [[NSMutableArray alloc] init];
	}
	return self;
}

-(NSMutableArray*)pixels
{
	return pixels;
}

-(void)reset
{
	[pixels removeAllObjects];
	[values removeAllObjects];
	pixelMass = 0;
}

-(NSString*)description
{
	return [NSString stringWithFormat:@"%@ %X center:%@ mass:%d",[self class],[self hash],NSStringFromPoint(center),pixelMass];
}

-(NSPoint)centerPoint
{
	return center;
}

-(int)pixelMass
{
	return pixelMass;
}

-(void)addPixel:(NSPoint)p 
{
	[self addPixel:p withValue:0xff];
}


-(void)addPixel:(NSPoint)p withValue:(unsigned char)value
{
	if (pixelMass == 0) {
		// first pixel
		xMax = xMin = p.x;
		yMax = yMin = p.y;
		valueMax = valueMin = value;
		center = p;
		pixelMass++;
		return;
	}
	// find the average center of the pixels
	center.x = ((center.x * pixelMass) + p.x) / (pixelMass + 1);
	center.y = ((center.y * pixelMass) + p.y) / (pixelMass + 1);
	pixelMass ++;
	if (p.x > xMax) xMax = p.x;
	if (p.y > yMax) yMax = p.y;

	if (p.x < xMin) xMin = p.x;
	if (p.y < yMin) yMin = p.x;
	
	if (value < valueMin) {
		valueMin = value;
	//	if (invert) center = p;		
	}
	if (value > valueMax) {
		valueMax = value;
	//	if (!invert) center = p;		
	}

	// you could add this in if you want to track the actual blob shape
	[pixels addObject:[NSValue valueWithPoint:p]];
	[values addObject:[NSNumber numberWithUnsignedChar:value]];
}

-(NSRect)boundingRect
{
	return NSMakeRect(xMin, yMin, xMax - xMin, yMax - yMin);
}

// jsut a convenience
-(float)radius
{
	return sqrt((float)pixelMass/3.14159);
}

@end
